package com.sanjiahantang.youjiaclient.module.city.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.module.city.beans.CityBean;
import com.sanjiahantang.youjiaclient.module.city.models.ICityModel;
import com.sanjiahantang.youjiaclient.module.city.views.CityView;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zy on 2017/9/19.
 */

public class CityAdapter extends BaseAdapter {

    public void setCityList(List<CityBean.City> cityList) {
        this.cityList = cityList;

        //排序
        Collections.sort(cityList, new ICityModel.CompareSort());
        cityList.add(0,new CityBean.City("定位","0"));

        int size = cityList.size();
        letterIndexes = new HashMap<>();
        sections = new String[size];
        for (int index = 0; index < size; index++){
            //当前城市拼音首字母
            String currentLetter = Utils.getFirstLetter(cityList.get(index).getPinyin());
            //上个首字母，如果不存在设为""
            String previousLetter = index >= 1 ? Utils.getFirstLetter(cityList.get(index - 1).getPinyin()) : "";
            if (!TextUtils.equals(currentLetter, previousLetter)){
                letterIndexes.put(currentLetter, index);
                sections[index] = currentLetter;
            }
        }

    }

    private List<CityBean.City> cityList;
    private LayoutInflater inflater;
    private HashMap<String, Integer> letterIndexes;
    private String[] sections;
    private static final int VIEW_TYPE_COUNT = 2;
    private Context mContext;
    private CityView.OnCityClickListener onCityClickListener;
    private String locatedCity;
    private int locateState;

    public CityAdapter(Context context, List<CityBean.City> cityList) {
        this.mContext = context;
        this.cityList = cityList;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return cityList == null ? 0 : cityList.size();
    }

    @Override
    public Object getItem(int position) {
        return cityList == null ? null : cityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        return position < VIEW_TYPE_COUNT - 1 ? position : VIEW_TYPE_COUNT - 1;
    }

    public static class CityViewHolder{
        TextView letter;
        TextView name;
        View line;
    }

    public void setOnCityClickListener(CityView.OnCityClickListener listener){
        this.onCityClickListener = listener;
    }


    /**
     * 获取字母索引的位置
     * @param letter
     * @return
     */
    public int getLetterPosition(String letter){
        Integer integer = letterIndexes.get(letter);
        return integer == null ? -1 : integer;
    }

    /**
     * 更新定位状态
     * @param state
     */
    public void updateLocateState(int state, String city){
        this.locateState = state;
        this.locatedCity = city;
        notifyDataSetChanged();
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CityViewHolder holder;
        int viewType = getItemViewType(position);
        switch (viewType){
            case 0:     //定位
                convertView = inflater.inflate(R.layout.item_city, parent, false);
                holder = new CityViewHolder();
                holder.letter = (TextView) convertView.findViewById(R.id.tv_item_city_listview_letter);
                holder.name = (TextView) convertView.findViewById(R.id.tv_item_city_listview_name);
                holder.letter.setText("定位城市");
                switch (locateState){
                    case CityView.LOCATING:
                        holder.name.setText("正在定位...");
                        break;
                    case CityView.FAILED:
                        holder.name.setText("定位失败");
                        break;
                    case CityView.SUCCESS:
                        holder.name.setText(locatedCity);
                        break;
                }
                holder.name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (locateState == CityView.FAILED){
                            //重新定位
                            if (onCityClickListener != null){
                                onCityClickListener.onLocateClick(mContext);
                            }
                        }else if (locateState == CityView.SUCCESS){
                            //返回定位城市
                            if (onCityClickListener != null){
                                CityBean.City cityLocate = new CityBean.City();
                                cityLocate.setRegion_name(locatedCity);
                                onCityClickListener.onCityClick(cityLocate);
                            }
                        }
                    }
                });
                break;
            case 1:    //所有
                if (convertView == null){
                    convertView = inflater.inflate(R.layout.item_city, parent, false);
                    holder = new CityViewHolder();
                    holder.letter = (TextView) convertView.findViewById(R.id.tv_item_city_listview_letter);
                    holder.name = (TextView) convertView.findViewById(R.id.tv_item_city_listview_name);
                    holder.line = convertView.findViewById(R.id.view_Line);
                    convertView.setTag(holder);
                }else{
                    holder = (CityViewHolder) convertView.getTag();
                }
                if (position >= 1){
                    final CityBean.City city = cityList.get(position);
                    holder.name.setText(city.getRegion_name());
                    String currentLetter = Utils.getFirstLetter(cityList.get(position).getPinyin());
                    String previousLetter = position >= 1 ? Utils.getFirstLetter(cityList.get(position - 1).getPinyin()) : "";
                    String nextLetter = position < getCount()-1? Utils.getFirstLetter(cityList.get(position + 1).getPinyin()) : "";
                    if (!TextUtils.equals(currentLetter, previousLetter)){
                        holder.letter.setVisibility(View.VISIBLE);
                        holder.letter.setText(currentLetter);
                    }else{
                        holder.letter.setVisibility(View.GONE);
                    }

                    if (!TextUtils.equals(currentLetter, nextLetter)){
                        holder.line.setVisibility(View.GONE);
                    }else{
                        holder.line.setVisibility(View.VISIBLE);
                    }
                    holder.name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onCityClickListener != null){
                                onCityClickListener.onCityClick(city);
                            }
                        }
                    });
                }
                break;
        }
        return convertView;
    }
}
